@echo off
chcp 65001 >nul
echo 根据图片清单复制文件...

set "LIST_FILE=temp\picknbuy2419941dd5b73c-fafe-49bb-ad.txt"
set "SOURCE_BASE=storage\vehicles"
set "TARGET_BASE=temp\copied_images"

if not exist "%LIST_FILE%" (
    echo 错误: 清单文件不存在 - %LIST_FILE%
    pause
    exit /b 1
)

if not exist "%SOURCE_BASE%" (
    echo 错误: 源目录不存在 - %SOURCE_BASE%
    pause
    exit /b 1
)

echo 创建目标目录...
if not exist "%TARGET_BASE%" mkdir "%TARGET_BASE%"

set /a TOTAL=0
set /a COPIED=0
set /a MISSING=0

echo 开始复制文件...
for /f "usebackq delims=" %%i in ("%LIST_FILE%") do (
    set /a TOTAL+=1
    
    set "SOURCE_FILE=%SOURCE_BASE%\%%i"
    set "TARGET_FILE=%TARGET_BASE%\%%i"
    
    call :copy_file "!SOURCE_FILE!" "!TARGET_FILE!"
)

echo.
echo 处理完成!
echo 总文件数: %TOTAL%
echo 成功复制: %COPIED%
echo 文件缺失: %MISSING%
pause
exit /b 0

:copy_file
setlocal enabledelayedexpansion
set "src=%~1"
set "dst=%~2"

if exist "%src%" (
    rem 创建目标目录
    for %%f in ("%dst%") do (
        if not exist "%%~dpf" mkdir "%%~dpf" 2>nul
    )
    
    rem 复制文件
    copy "%src%" "%dst%" >nul 2>&1
    if !errorlevel! equ 0 (
        set /a COPIED+=1
        if !COPIED! gtr 0 (
            set /a mod=!COPIED! %% 100
            if !mod! equ 0 echo 已复制 !COPIED! 个文件...
        )
    )
) else (
    set /a MISSING+=1
    echo 文件不存在: %src%
)
endlocal
goto :eof
